// Copyright 2013 Square, Inc.

package com.idle.view;

import com.idle.android.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class TouchFocusView extends ImageView {

	private Paint paint = new Paint(Paint.SUBPIXEL_TEXT_FLAG
          |Paint.ANTI_ALIAS_FLAG);
  	final int LINENUM = 2;
	private float fx;
	private float fy;
	int length = 80;
	
	Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.touch_focus);
	private boolean isIconVisible = false;
	private boolean isAutoDismiss = true;
	
	public boolean isIconVisible() {
		return isIconVisible;
	}

	public void setIconVisible(boolean isIconVisible) {
		this.isIconVisible = isIconVisible;
		invalidate();
	}

	public boolean isAutoDismiss() {
		return isAutoDismiss;
	}

	public void setAutoDismiss(boolean isAutoDismiss) {
		this.isAutoDismiss = isAutoDismiss;
	}

	public TouchFocusView(Context context) {
		super(context);
	}

	public TouchFocusView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public TouchFocusView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

  

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		fx = event.getX();
        fy = event.getY();
        isIconVisible = true;
        invalidate();
        if (onTouchInterface != null) {
        	onTouchInterface.OnTouch(fx, fy);
		}
        if (isAutoDismiss) {
        	new Handler().postDelayed(new Runnable() {
    			
    			@Override
    			public void run() {
    				isIconVisible = false;
    		        invalidate();
    			}
    		}, 800);
		}
        
		return super.onTouchEvent(event);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		paint.setColor(0xffffffff);
		paint.setStrokeWidth(6f);
		/*int width = getWidth();
		int height = getHeight();*/
		
		/*canvas.drawLine(fx-length, fy-length, fx+length, fy-length, paint);
		canvas.drawLine(fx-length, fy+length, fx+length, fy+length, paint);
		canvas.drawLine(fx-length, fy-length, fx-length, fy+length, paint);
		canvas.drawLine(fx+length, fy-length, fx+length, fy+length, paint);*/
		
		/*canvas.drawLine(fx-length, fy-length, fx-length+40, fy-length, paint);
		canvas.drawLine(fx+length-40, fy-length, fx+length, fy-length, paint);
		
		canvas.drawLine(fx-length, fy+length, fx-length+40, fy+length, paint);
		canvas.drawLine(fx+length-40, fy+length, fx+length, fy+length, paint);
		
		canvas.drawLine(fx-length, fy-length, fx-length, fy-length+40, paint);
		canvas.drawLine(fx-length, fy+length-40, fx-length, fy+length, paint);
		
		canvas.drawLine(fx+length, fy-length, fx+length, fy-length+40, paint);
		canvas.drawLine(fx+length, fy+length-40, fx+length, fy+length, paint);*/
		if (isIconVisible) {
			canvas.drawBitmap(bmp, fx-length, fy-length, paint);
		}
	}

	OnTouchInterface onTouchInterface;
	public void setOnTouchInterface(OnTouchInterface onTouchInterface) {
		this.onTouchInterface = onTouchInterface;
	}
	public interface OnTouchInterface{
		void OnTouch(float fx, float fy);
	}
}
